package com.bw.redis;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

/**
 * Created by qpx on 2018/5/11.
 */
public class RedisObjectSerializer implements RedisSerializer<Object> {


    private Converter<Object, byte[]> serializer = new SerializingConverter();

    private Converter<byte[],Object> deserializing = new DeserializingConverter();
    @Override
    public byte[] serialize(Object o) throws SerializationException {
        if(o==null){
            return null;
        }
        return serializer.convert(o);
    }

    @Override
    public Object deserialize(byte[] bytes) throws SerializationException {
        if (bytes==null){
            return null;
        }
        return deserializing.convert(bytes);
    }
}
